package com.util;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class ExcelUtil extends AbstractExcelView {

	@SuppressWarnings("unchecked")
	@Override
	protected void buildExcelDocument(
			Map<String, Object> model,
			HSSFWorkbook workbook,
			HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		
		// TODO Auto-generated method stub

		Map<String, Object> commandMap = (Map<String, Object>) model.get("commandMap");
		ArrayList<Map<String, Object>> listMap = (ArrayList<Map<String, Object>>) model.get("listMap");
		ArrayList<String> firstRowList = (ArrayList<String>) model.get("firstRowList");
		
		String userAgent = req.getHeader("User-Agent");
		String fileName =  commandMap.get("title").toString() + ".xls";

		if (userAgent.indexOf("MSIE") > -1) 
		{
			fileName = URLEncoder.encode(fileName, "utf-8");
		}
		else 
		{
			fileName = new String(fileName.getBytes("utf-8"), "iso-8859-1");
		}

		res.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\";");
		res.setHeader("Content-Transfer-Encoding", "binary");
		
		//시트 생성
		HSSFSheet sheet = createFirstSheet(workbook);
		
		//컬럼영 작성
		createColumnLabel(sheet, firstRowList);

		//데이터 작성
		for (int i = 0; i < listMap.size(); i++) 
		{
			createPageRow(sheet, listMap.get(i), i, commandMap);
		}
	}

	private HSSFSheet createFirstSheet(HSSFWorkbook workbook) {
		HSSFSheet sheet = workbook.createSheet();
		//workbook.setSheetName(0, "테스트");
		//sheet.setColumnWidth(1, 256 * 30);
		
		return sheet;
	}

	private void createColumnLabel(HSSFSheet sheet, ArrayList<String> firstRowList) {
		HSSFRow firstRow = sheet.createRow(0);
		
		HSSFCell cell = firstRow.createCell(0);
		cell.setCellValue(firstRowList.get(0));
		
		for (int i = 1; i < firstRowList.size(); i++) 
		{
			cell = firstRow.createCell(i);
			cell.setCellValue(firstRowList.get(i));
		}
	}

	private void createPageRow(HSSFSheet sheet, Map<String, Object> map, int rowNum, Map<String, Object> commandMap) {
		HSSFRow row = sheet.createRow(rowNum + 1);

		HSSFCell cell = row.createCell(0);
		
		if(commandMap.get("type").equals("bbs"))
		{
			cell.setCellValue(map.get("BBS_NAME").toString());
			
			cell = row.createCell(1);
			cell.setCellValue(map.get("TOTALCOUNT").toString());
			
			cell = row.createCell(2);
			cell.setCellValue(map.get("TOTAL").toString());
		}
		else
		{
			cell.setCellValue(map.get("INPUT_DATE").toString());
			
			cell = row.createCell(1);
			cell.setCellValue(map.get("TOTAL").toString());
			
			cell = row.createCell(2);
			cell.setCellValue(map.get("PERC").toString());
		}
	}
}
